﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WXshopBuiness;
using Services.WXshopBuiness;
using Services.Util;
using Model.Util;
using Xproject.Util;
using System.IO;
using Services.WXProjectAdmin;
using Model.Admin;
using Model.WXshopBuiness.Enum;
using Model.WeiXin.DTO;
using Core.WeiXin;
using Model.Const;

namespace WeiXinProject.Controllers
{
    public class ShopController : WXBaseController
    {
        WXProductServices m_WXProductServices;
        UploadFileServices m_UploadFileServices;
        SystemAdServices m_SystemAdServices;
        WXOrderServices m_WXOrderServices;
        UserOperateServices m_UserOperateServices;
        UserCollectionServices m_UserCollectionServices;
        IndexCategoryServices m_IndexCategoryServices;
        public ShopController(IndexCategoryServices mIndexCategoryServices, UserCollectionServices mUserCollectionServices,UserOperateServices mUserOperateServices, WXOrderServices mWXOrderServices,WXProductServices mWXProductServices, UploadFileServices mUploadFileServices, SystemAdServices mSystemAdServices)
        {
            m_WXProductServices = mWXProductServices;
            m_UploadFileServices = mUploadFileServices;
            m_SystemAdServices = mSystemAdServices;
            m_WXOrderServices = mWXOrderServices;
            m_UserOperateServices = mUserOperateServices;
            m_UserCollectionServices=   mUserCollectionServices;
            m_IndexCategoryServices = mIndexCategoryServices;
        }

        // GET: Shop
        public ActionResult Index(int? limit,int? offset)
        {             
            List<Advertisement> ads = m_SystemAdServices.GetTopNAdvertisement(Site, 4, "QDSYLB");
            List<IndexCategory> indexcategory  =  m_IndexCategoryServices.GetTopNIndexCategorys(4);
            List<WXProduct> tuijianProducts = new List<WXProduct>();
            tuijianProducts =m_WXProductServices.GetWXProductTuiJian(Site, 3);
            ViewData["TUIJIAN"] = tuijianProducts;
            if (!limit.HasValue)
            {
                limit = 10;
            }
            if (!offset.HasValue)
            {
                offset = 0;
            }
            List<WXProduct> indexProducts = new List<WXProduct>();
           // indexProducts =  m_WXProductServices.GetWXProductIndex(Site, limit, offset);
           // ViewData["INDEXLIST"] = indexProducts;
            ViewData["ads"] = ads;
            ViewData["IndexCategory"] = indexcategory;
            return View();
        }

        public JsonResult GetIndexProductList(int? index, int? pagesize)
        {
            var jsonData = new Object();
            List<WXProduct> model = new List<WXProduct>();                                     
            model = m_WXProductServices.GetWXProductIndex(Site,  pagesize, index); 
            jsonData = new { Success = true, Data = model };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ProductDetail(string productId)
        {

            ViewBag.IsFoucs = this.CurrentUser.IsFocus == 1 ? true : false;
            WXProduct product = m_WXProductServices.GetById(productId);
            if (product == null)
            {
               return Redirect("/CommonBase/WarningNotice?Notice=" + "对不起，您浏览的产品不存在！");
            }
            if (product.State == 1)
            {
                return Redirect("/CommonBase/WarningNotice?Notice=" + "对不起，您此宝贝已下架！");
            }
            if (product.State == 2)
            {
                return Redirect("/CommonBase/WarningNotice?Notice=" + "对不起，此宝贝已不是最新的啦。到首页转转吧");
            }
           

            ViewBag.zxUserId = CurrentUser.ZX_UserID;
            string wxJsApiParam = WeiXinHelper.GetWxConfig();
            ViewBag.wxJsApiParam = wxJsApiParam;
         
            m_UserOperateServices.AddOperateLog(EnumOperateType.浏览产品, this.CurrentUser.UserID, productId, GetDomain, "/Shop/ProductDetail?productId=" + productId, "浏览产品 ："+product.ProductName);
            m_WXOrderServices.UpdatePruductVisitCountInfo(productId);
            List<string> Images = new List<string>();
           List<ProductContents> contents = new List<ProductContents>();
           List<ProductComments> comments = new List<ProductComments>();
           List<WeiXinProductAttrShowModel> productAttrs = new List<WeiXinProductAttrShowModel>();
           Dictionary<int,string> CateGroup = new Dictionary<int, string>();
            
            string userId =this.CurrentUser.UserID;
            if (product != null)
            {
                if (!string.IsNullOrEmpty(product.ImageUrl1)) { Images.Add( product.Domain + product.ImageUrl1); }
                if (!string.IsNullOrEmpty(product.ImageUrl2)) { Images.Add(product.Domain + product.ImageUrl2); }
                if (!string.IsNullOrEmpty(product.ImageUrl3)) { Images.Add(product.Domain + product.ImageUrl3); }
                if (!string.IsNullOrEmpty(product.ImageUrl4)) { Images.Add(product.Domain + product.ImageUrl4); }
                if (!string.IsNullOrEmpty(product.ImageUrl5)) { Images.Add(product.Domain + product.ImageUrl5); }
                if (!string.IsNullOrEmpty(product.ImageUrl6)) { Images.Add(product.Domain + product.ImageUrl6); }
                if (!string.IsNullOrEmpty(product.ImageUrl7)) { Images.Add(product.Domain + product.ImageUrl7); }
                if (!string.IsNullOrEmpty(product.ImageUrl8)) { Images.Add(product.Domain + product.ImageUrl8); }
                if (!string.IsNullOrEmpty(product.ImageUrl9)) { Images.Add(product.Domain + product.ImageUrl9); }                 
            }

            contents =  m_WXProductServices.GetProductContent(productId);
            comments =  m_WXProductServices.GetTopNComments(Site, productId, 20);
            productAttrs = m_WXProductServices.GetProductAttrsF(productId);
            //WeiXinProductAttrShowModel KDFattr = m_WXProductServices.GetProductDefaultKDF(productId);
            //if (KDFattr == null)
            //{
            //    KDFattr = new WeiXinProductAttrShowModel();
            //}
            if (productAttrs != null && productAttrs.Count > 0)
            {
                foreach (WeiXinProductAttrShowModel m in productAttrs)
                {
                    if (!CateGroup.ContainsKey(m.AttrGroupId)) { 
                    CateGroup.Add(m.AttrGroupId,m.AttrGroupName);
                    }
                }
                
            }
            List<ProductSpecification> specification = m_WXProductServices.GetProductSpecList(productId);
            if (specification != null&&specification.Count>0)
            {
                ViewBag.HasSpeci = true;
            }
            else
            {
                ViewBag.HasSpeci = false;
            }
            ViewData["ProductSpec"] = specification;
            ViewData["CateGroup"] = CateGroup;
            ViewData["ScrollImages"] = Images;
            ViewData["contents"] = contents;
            ViewData["comments"] = comments;
            
            ViewData["productAttrs"] = productAttrs;
            var usermodel =Cache_UserInfoModel.GetUserInfoModel(userId);
            if (usermodel == null)
            {
                usermodel =  new UserInfoModel();
                usermodel.UserID = userId;
                usermodel.UserCode = this.CurrentUser.UserCode;
                usermodel.HeadUrl = this.CurrentUser.HeadUrl;
                usermodel.NickName = this.CurrentUser.NickName;
                usermodel.orderinfo = new UserOrderInfo();               
                usermodel.orderinfo.ShopCarNeedToPayCount = m_WXOrderServices.GetUserShoppingCarCount(userId) ;
                usermodel.orderinfo.ShopCarNeedToPayTotal = m_WXOrderServices.GetUserShoppingCarMoney(userId);

                List<WeiXinOrder> orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITINGPAY);
                usermodel.orderinfo.NeedToPayCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.PAYED);
                usermodel.orderinfo.ShopCarPayedSuccessCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.SHIPPING);
                usermodel.orderinfo.ShopCarShippingCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.FINISHED);
                usermodel.orderinfo.ShopCarDoneCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITCOMMENT);
                usermodel.orderinfo.ShopCarWaitingComment = orders.Count;
                Cache_UserInfoModel.SetUserInfoModelCache(userId, usermodel);
            }
            else
            {
                if (usermodel.orderinfo ==null)
                {
                    usermodel.orderinfo = new UserOrderInfo();                    
                    usermodel.orderinfo.ShopCarNeedToPayCount = m_WXOrderServices.GetUserShoppingCarCount(userId);
                    usermodel.orderinfo.ShopCarNeedToPayTotal = m_WXOrderServices.GetUserShoppingCarMoney(userId);
                    List<WeiXinOrder> orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITINGPAY);
                    usermodel.orderinfo.NeedToPayCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.PAYED);
                    usermodel.orderinfo.ShopCarPayedSuccessCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.SHIPPING);
                    usermodel.orderinfo.ShopCarShippingCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.FINISHED);
                    usermodel.orderinfo.ShopCarDoneCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITCOMMENT);
                    usermodel.orderinfo.ShopCarWaitingComment = orders.Count;
                    Cache_UserInfoModel.SetUserInfoModelCache(userId, usermodel);
                }
            }

            ViewBag.usermodel = usermodel;
            ViewBag.hasvideo = !string.IsNullOrEmpty(product.VideoUrl);

            if (!string.IsNullOrEmpty(product.ShowStyle))
            {
                if (product.ShowStyle.ToString() == ((int)EnumProductShowType.纯图).ToString())
                {
                    return View("~/Views/Shop/ProductDetail.cshtml", product);
                  
                }
                if (product.ShowStyle.ToString() == ((int)EnumProductShowType.图文).ToString())
                {
                    return View("~/Views/Shop/ProductDetailS1.cshtml", product);
                }
            }
            else
            {
                return View(product);
            }
            return View(product);
        }

        public ActionResult PaySelect(string orderId)
        {
            ViewBag.orderId = orderId;
            return View();
        }

        public JsonResult AddProductToMyCollection(string productId)
        {
            var jsonData = new Object();
            WXProduct product = m_WXProductServices.GetById(productId);
            string message = string.Empty;
            if (!m_UserCollectionServices.AddUserCollection(this.CurrentUser.UserID, productId, GetDomain + "/Shop/ProductDetail?productId=" + productId, product.ProductName,
                product.ShortDesc, product.Domain + product.ThumbnailUrl1, ref message))
            {
                jsonData = new { Success = false, Data = message };
            }
            else
            {
                jsonData = new { Success = true, Data = "已成功添加到我的收藏！" };
            }             
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CateProList(int cateId)
        {
            ViewBag.cateId = cateId;
            return View();
        }

        public JsonResult GetCateProductList(int? index, int? pagesize,int cateId)
        {
            var jsonData = new Object();
            List<WXProduct> model = new List<WXProduct>();
            model = m_WXProductServices.GetCategoryWXProduct(Site,cateId, pagesize, index);
            jsonData = new { Success = true, Data = model };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ShopCar()
        {
            ViewBag.OrderState = (int)Enum_OrderState.SHOPCAR;
            return View();
        }

        public ActionResult add()
        {
            return View();
        }

        public ActionResult add_add()
        {
            return View();
        }

        public ActionResult address()
        {
            return View();
        }

        public ActionResult apply()
        {
            return View();
        }
        public ActionResult apply1()
        {
            return View();
        }
        public ActionResult bank()
        {
            return View();
        }

        public ActionResult bill()
        {
            return View();
        }
        public ActionResult business()
        {
            return View();
        }
        public ActionResult capital()
        {
            return View();
        }
        public ActionResult center()
        {
            return View();
        }
        public ActionResult collection()
        {
            return View();
        }
        public ActionResult commodity()
        {
            return View();
        }
        public ActionResult convenience()
        {
            return View();
        }

        public ActionResult credit()
        {
            return View();
        }

        public ActionResult data()
        {
            return View();
        }

        public ActionResult detailed()
        {
            return View();
        }

        public ActionResult details()
        {
            return View();
        }

        public ActionResult details_shop()
        {
            return View();
        }

        public ActionResult footprint()
        {
            return View();
        }
        public ActionResult forget()
        {
            return View();
        }
        public ActionResult forget1()
        {
            return View();
        }
        public ActionResult information()
        {
            return View();
        }
        public ActionResult login()
        {
            return View();
        }
        public ActionResult modify()
        {
            return View();
        }
        public ActionResult modify1()
        {
            return View();
        }
        public ActionResult order()
        {
            return View();
        }
        public ActionResult order_shop()
        {
            return View();
        }
        public ActionResult payment()
        {
            return View();
        }
        public ActionResult personal()
        {
            return View();
        }
        public ActionResult phone()
        {
            return View();
        }
        public ActionResult phone1()
        {
            return View();
        }
        public ActionResult profit()
        {
            return View();
        }
        public ActionResult quota()
        {
            return View();
        }
        public ActionResult recharge()
        {
            return View();
        }
        public ActionResult recommend()
        {
            ViewBag.UserCode = CurrentUser.UserCode;
            return View();
        }
        public ActionResult register()
        {
            return View();
        }
        public ActionResult search()
        {
            return View();
        }
        public ActionResult search1()
        {
            return View();
        }
        public ActionResult set()
        {
            return View();
        }
        public ActionResult set1()
        {
            return View();
        }
        public ActionResult shop()
        {
            return View();
        }
        public ActionResult wallet()
        {
            return View();
        }
        public ActionResult wdetailed()
        {
            return View();
        }
        public ActionResult withdrawals()
        {
            return View();
        }
        
    }
}